const db = require('../../db.config')
const Sequelize = db.sequelize
const Op = Sequelize.Op

const likeClassify = Sequelize.import('../../schema/recommend/likeClassify')

likeClassify.sync({force: false})

class LikeClassifyModule {
  /**
   * 新增偏好
   */
  static async setLikeClassify(data) {
    likeClassify.create({
      userId: data.userId,
      classifyKey: data.classifyKey
    })
  }
  
  /**
   * 查询偏好
   */
  static async getLikeClassify(where) {
    return await likeClassify.findOne({
      where,
      raw: true
    })
  }

  /**
   * 查询所有偏好
   */
  static async getAllLikeClassify(where) {
    return await likeClassify.findAll({
      where,
      raw: true
    })
  }

  /**
   * 更新偏好
   */
  static async updateLikeClassify(where, data) {
    let item = await likeClassify.findOne({
      where
    })
    item.classifyKey = data.classifyKey
    await item.save()
    return item
  }
}

module.exports = LikeClassifyModule
